//
//  RestListTableCustomCell.m
//  iBaby365v2
//
//  Created by admin on 14/12/17.
//  Copyright (c) 2014年 suplus. All rights reserved.
//
// 昵称的字体
#define NameFont [UIFont systemFontOfSize:15]
// 正文的字体
#define TextFont [UIFont systemFontOfSize:13]
#define timeFont [UIFont systemFontOfSize:12]
#import "RestListTableViewCell.h"

#import "NSDate+suplus.h"
#import "UIImageView+AFNetworking.h"
#import "NetworkAccessManager.h"
#import "UIAlertView+Kirinrin.h"

@interface RestListTableViewCell ()
@property(nonatomic,strong) NSArray *reverMutableArray;
@end
@implementation RestListTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"restClassCell";
    RestListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[RestListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    NSLog(@"--cell.subviews -----%@--",cell.contentView.subviews);
    
    for (UIView *child in cell.contentView.subviews) {
        NSLog(@"=====child==%@",child);
        if(child.tag == 30){
            while ([child.subviews lastObject] != nil) {
                [[child.subviews lastObject]removeFromSuperview];
            }
        }
        
    }
    
    
    return cell;
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if(self){
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.photo = iconView;
        UILabel *nameView = [[UILabel alloc]init];
        nameView.font =NameFont;
        [self.contentView addSubview:nameView];
        self.name = nameView;
        UILabel *dateDimeView = [[UILabel alloc]init];
        dateDimeView.textColor = [UIColor lightGrayColor];
        dateDimeView.font =timeFont;
        [self.contentView addSubview:dateDimeView];
        self.dateTime = dateDimeView;

        UILabel *contentLableView = [[UILabel alloc]init];
        contentLableView.numberOfLines = 0;
        contentLableView.font =TextFont;
        [self.contentView addSubview:contentLableView];
        
        self.content = contentLableView;
        
        UIView *revertsView = [[UIView alloc]init];
        revertsView.tag = 30;
        [self.contentView addSubview:revertsView];
        self.revertView = revertsView;
        
        
    }
    return self;
}

-(void)setRestListCellFrame:(RestListCellFrame *)restListCellFrame
{
    
    _restListCellFrame = restListCellFrame;
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if ([restListCellFrame.data[kPhoto]isEqualToString:appDelegate.user[@"photo"]]) {
        [_photo setImage:appDelegate.userPhoto];
    }else{
        [_photo setImageWithURL:[NSURL URLWithString:restListCellFrame.data[kPhoto]]placeholderImage:[UIImage imageNamed:@"imageUserPhotoDefault"]];
    }
    
    _photo.frame = restListCellFrame.iconF;
    
    _name.text = restListCellFrame.data[kparentName];
    _name.frame = restListCellFrame.nameF;
    
    NSString *dateStringWithT = restListCellFrame.data[kCreateTime];
    NSString *dateString = [dateStringWithT stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    
    _dateTime.text = [restListCellFrame dateTimeTC:dateString];
    _dateTime.frame = restListCellFrame.dateTimeF;
    
    _content.text = restListCellFrame.data[kContent];
    _content.frame = restListCellFrame.contentF;
    
//    _revert.frame = restListCellFrame.revrtBUttonF;
    
    
    
    self.reverMutableArray = restListCellFrame.reverMutableArray;
    NSLog(@"----reverMutableArray---->%d",self.reverMutableArray.count);
    NSArray *reverMutableArrayF = _restListCellFrame.reViewMutableArrayF;
    if(_reverMutableArray.count>0 && _reverMutableArray.count == reverMutableArrayF.count){
        _revertView.hidden = NO;
        for (int i=0 ;i< _reverMutableArray.count;i++) {
            //      for (NSDictionary *dict in _reverMutableArray) {
            NSDictionary *dictF =reverMutableArrayF[i];
            NSDictionary *dict =_reverMutableArray[i];
            NSString *replyContent = dict[@"content"];
            NSString *createTime = dict[@"createDime"];
            
            NSString *timeText = [restListCellFrame dateTimeTC:createTime];
            
            UILabel *replyContentView = [[UILabel alloc]init];
            
            replyContentView.font =TextFont;
            replyContentView.text =@"回复:";
            replyContentView.textColor = UIColorFromRGB(0x027282, 1.0);
            [_revertView addSubview:replyContentView];
            
            replyContentView.frame =[dictF[@"replyContentFValue"]CGRectValue];
            
            UILabel *timeContentView = [[UILabel alloc]init];
            timeContentView.font =timeFont;
            timeContentView.text =timeText;
            timeContentView.textColor = [UIColor lightGrayColor];
            [_revertView addSubview:timeContentView];
            
            timeContentView.frame = [dictF[@"timeContentFValue"]CGRectValue];
            
            
            UILabel *revertContentView = [[UILabel alloc]init];
            revertContentView.numberOfLines = 0;
            revertContentView.font =TextFont;
            revertContentView.text =replyContent;
            [_revertView addSubview:revertContentView];
            
            revertContentView.frame = [dictF[@"revertContentFValue"]CGRectValue];
            
        }
        _revertView.frame = _restListCellFrame.revertViewF;
    }else{
        _revertView.hidden = YES;
    }
}

-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);
    
    //    //上分割线，
    //    CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"ffffff"].CGColor);
    //    CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1));
    
    //下分割线
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:49.0/255.0 green:48.0/255.0 blue:46.0/255.0 alpha:0.2].CGColor);
    CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10, 1));
    
    
}


/**
 *  计算文字尺寸
 *
 *  @param text    需要计算尺寸的文字
 *  @param font    文字的字体
 *  @param maxSize 文字的最大尺寸
 */
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

@end
